<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商家名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入店铺名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.createdTime"
          type="daterange"
          value-format="yyyy-MM-dd"
          start-placeholder="开始时间"
          end-placeholder="结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['union:recommend:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-refresh-right"
          size="mini"
          @click="handleAddNew"
          v-hasPermi="['union:recommend:addNew']">
          增加五条推荐
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recommendList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="联盟商家" align="center" prop="shop_name" />
      <el-table-column label="排序号" align="center" prop="sort" />
      <el-table-column label="是否推荐" align="center" prop="status" :formatter="formatStatus" />
      <el-table-column label="创建时间" align="center" prop="created_time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.created_time, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['union:recommend:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['union:recommend:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改联盟商家 APP前端=推荐关系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item v-if="form.shopId === null" label="店铺名称" prop="shopId">
          <el-select
            v-model="form.shopId"
            placeholder="请选择店铺"
            clearable
            filterable
            remote
            reserve-keyword
            :remote-method="getShopList">
            <el-option
              v-for="item in shopOptions"
              :key="item.shopId"
              :label="item.shopName"
              :value="item.shopId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序号" prop="sort">
          <el-input-number :min="1" v-model="form.sort" placeholder="请输入排序号" />
        </el-form-item>
        <el-form-item label="过期时间" prop="dueDate">
          <el-date-picker clearable size="small"
            v-model="form.dueDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择过期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="推荐">
          <el-radio-group v-model="form.status">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRecommend,
  getRecommend,
  delRecommend,
  addRecommend,
  updateRecommend,
  addNew
} from '@/api/union/recommend'
import { getShopList } from '@/api/app/messageDefine'

export default {
  name: "Recommend",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 联盟商家 APP前端=推荐关系表格数据
      recommendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createdTime: []
      },
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sort: [
          { required: true, message: "排序号不能为空", trigger: "blur" }
        ],
        dueDate: [
          { required: true, message: "过期时间不能为空", trigger: "blur" }
        ]
      },
      shopOptions: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询联盟商家 APP前端=推荐关系列表 */
    getList() {
      this.loading = true;

      if (this.queryParams.createdTime) {
        this.queryParams.startTime = this.queryParams.createdTime[0]
        this.queryParams.endTime = this.queryParams.createdTime[1]
      }

      listRecommend(this.queryParams).then(response => {
        this.recommendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        shopId: null,
        sort: null,
        dueDate: null,
        status: "1",
        createdTime: null,
        createdUser: null,
        updatedTime: null,
        updatedUser: null
      };
      this.shopOptions = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.shop_id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加推荐商家";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const shopId = row.shop_id || this.ids
      getRecommend(shopId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改推荐商家 - " + row.shop_name;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.shop_id != null) {
            updateRecommend(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecommend(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const shopIds = row.shop_name || this.ids;
      this.$confirm('是否确认删除店铺推荐编号为"' + shopIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRecommend(shopIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    formatStatus(row, column, cellValue) {
      if (row.status === "1") {
        return "是"
      }
      return "否"
    },
    handleAddNew() {
      addNew().then(response => {
        if (response.data === true) {
          this.getList();
          this.msgSuccess("增加数据成功");
        }
      })
    },
    getShopList(query) {
      if (query !== '') {
        setTimeout(() => {
          getShopList(query).then(response => {
            this.shopOptions = response.data
          })
        }, 200)
      }
    },
  }
};
</script>
